package com.itbuka.vip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itbuka.vip.domain.Vip;
import com.itbuka.vip.mapper.VipMapper;
import com.itbuka.vip.service.VipService;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.List;

/**
 * @author ld
 * @description 针对表【vip】的数据库操作Service实现
 * @createDate 2024-07-28 12:37:06
 */
@Service
public class VipServiceImpl implements VipService {
    @Autowired
    private VipMapper vipMapper;

    @Override
    public List<Vip> selectAll() {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Vip::getIsDelete, 0);
        return vipMapper.selectList(lqw);
    }

    @Override
    public List<Vip> selectList(Vip vip) {
        LambdaQueryWrapper<Vip> lqw = this.lqw(vip);
        return vipMapper.selectList(lqw);
    }

    @Override
    public Integer insert(Vip vip) {
        return vipMapper.insert(vip);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                Vip vip = new Vip();
                vip.setId(Long.parseLong(id));
                vip.setIsDelete(1);
                vipMapper.updateById(vip);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(Vip vip) {
        return vipMapper.updateById(vip);
    }

    @Override
    public Page<Vip> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<Vip>) this.selectAll();
    }

    @Override
    public Page<Vip> pageList(Vip vip, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<Vip>) this.selectList(vip);
    }

    @Override
    public Integer status(Long id, Integer status) {
        if (id == null) {
            return -1;
        }
        if (!(status == 0 | status == 1)) {
            return -2;
        }
        Vip vip = new Vip();
        vip.setId(id);
        vip.setStatus(status);
        return vipMapper.updateById(vip);
    }

    public LambdaQueryWrapper<Vip> lqw(Vip vip) {
        LambdaQueryWrapper<Vip> lqw = new LambdaQueryWrapper<>();
        if (vip.getId() != null) {
            lqw.eq(Vip::getId, vip.getId());
        }
        if (vip.getVipName() != null) {
            lqw.eq(Vip::getVipName, vip.getVipName());
        }
        if (vip.getVipNickname() != null) {
            lqw.eq(Vip::getVipNickname, vip.getVipNickname());
        }
        if (vip.getVipPhone() != null) {
            lqw.eq(Vip::getVipPhone, vip.getVipPhone());
        }
        if (vip.getVipPoints() != null) {
            lqw.eq(Vip::getVipPoints, vip.getVipPoints());
        }
        if (vip.getStatus() != null) {
            lqw.eq(Vip::getStatus, vip.getStatus());
        }
        if (vip.getIsDelete() != null) {
            lqw.eq(Vip::getIsDelete, vip.getIsDelete());
        }
        if (vip.getCreateTime() != null) {
            lqw.eq(Vip::getCreateTime, vip.getCreateTime());
        }
        if (vip.getUpdateTime() != null) {
            lqw.eq(Vip::getUpdateTime, vip.getUpdateTime());
        }
        return lqw;
    }
}




